package com.ldy.demo8;

import com.meterware.httpunit.*;
import com.sun.deploy.net.HttpResponse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.xml.sax.SAXException;
import sun.net.www.http.HttpClient;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

public class HttpunitTest1 {

    public static void main(String[] args) {
        try {
//            HttpunitTest1.testGetHtmlContent();
            //HttpunitTest1.testGetMethod();
            //HttpunitTest1.testPostMethod();
            //HttpunitTest1.testFormSubmit();
            HttpunitTest1.testClickLink();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testGetHtmlContent() throws IOException, SAXException{
        System.out.println("直接获取网页内容：");
        HttpUnitOptions.setScriptingEnabled(false);
        // 建立一个WebConversation实例    
        WebConversation wc = new WebConversation();
        // 向指定的URL发出请求，获取响应    
        WebResponse wr = wc.getResponse("http://www.baidu.com.cn");
        // 用getText方法获取相应的全部内容    
        // 用System.out.println将获取的内容打印在控制台上    
        System.out.println(wr.getText());
    }

    /**
     * 用get方法获取页面内容  
     * @throws MalformedURLException
     * @throws IOException
     * @throws SAXException
     */
    public static void testGetMethod() throws MalformedURLException,
            IOException, SAXException {
        System.out.println("向服务器发送数据，然后获取网页内容：");
        // 建立一个WebConversation实例    
        WebConversation wc = new WebConversation();
        // 向指定的URL发出请求    
        WebRequest req = new GetMethodWebRequest("http://localhost:8080/test.html");
        // 给请求加上参数    
        req.setParameter("query", "四氯化碳");
        // 获取响应对象    
        WebResponse resp = wc.getResponse(req);
        // 用getText方法获取相应的全部内容    
        // 用System.out.println将获取的内容打印在控制台上    
        System.out.println(resp.getText());

    }

    /**
     * 用post方法获取页面内容  
     * @throws MalformedURLException
     * @throws IOException
     * @throws SAXException
     */
    public static void testPostMethod() throws MalformedURLException,
            IOException, SAXException {
        System.out.println("使用Post方式向服务器发送数据，然后获取网页内容：");
        // 建立一个WebConversation实例    
        WebConversation wc = new WebConversation();
        // 向指定的URL发出请求    
        WebRequest req = new PostMethodWebRequest("http://baidu.com/lii");
        // 给请求加上参数    
        req.setParameter("user_name", "test");
        req.setParameter("password", "111111");
        // 获取响应对象    
        WebResponse resp = wc.getResponse(req);

        // 用getText方法获取相应的全部内容    
        // 用System.out.println将获取的内容打印在控制台上    
        System.out.println(resp.getText());
    }

    /**
     * 获取页面链接并模拟点击  
     * @throws MalformedURLException
     * @throws IOException
     * @throws SAXException
     */
    public static void testClickLink() throws MalformedURLException,
            IOException, SAXException {
        System.out.println("获取页面中链接指向页面的内容：");
        // 建立一个WebConversation实例    
        WebConversation wc = new WebConversation();
        // 获取响应对象    
        WebResponse resp = wc.getResponse("http://www.265.com/");
        // 获得页面链接对象    
        WebLink link = resp.getLinkWith("土豆");
        // 模拟用户单击事件    
        link.click();
        // 获得当前的响应对象    
        WebResponse nextLink = wc.getCurrentPage();

        // 用getText方法获取相应的全部内容    
        // 用System.out.println将获取的内容打印在控制台上    
        System.out.println(nextLink.getText());

    }

    /**
     * 测试WebForm的处理表单和提交能力 
     */
    public static void testFormSubmit(){
        HttpUnitOptions.setScriptingEnabled(false);
        WebConversation wc = new WebConversation();
        WebRequest request = new PostMethodWebRequest("http://baidu.com/li/");
        WebResponse response = null;
        try {
            response = wc.getResponse(request);
            //获得Html中的form表单，HttpUnit将他封装成WebForm对象  
            WebForm form = response.getForms()[0];
            //WebForm对象提供getParameterValue的方法将根据表单中的name获得对应的value,而不用管该元素的类型。  
            //对表单进行处理操作  
            form.setParameter("username", "le");
            form.setParameter("password", "le");
            //提交表单 获得新的response  
            response = form.submit();
            System.out.println(response.getText());
            System.out.println("----------------------------");
            // 获得页面链接对象    
            WebLink link = response.getLinkWith("新闻中心");
            //模拟用户单击事件    
            link.click();
            // 获得当前的响应对象    
            WebResponse nextLink = wc.getCurrentPage();
            // 用getText方法获取相应的全部内容    
            // 用System.out.println将获取的内容打印在控制台上    
            System.out.println(nextLink.getText());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void tet1() throws Exception {
        //创建默认的httpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            //用get方法发送http请求
            HttpGet get = new HttpGet("https://makeabooking.flyscoot.com/tgrairwaysdstl.js?PID=8FAAEB31-5110-3491-80DF-81C3207C174B");
            System.out.println("执行get请求:...."+get.getURI());
            CloseableHttpResponse httpResponse = null;
            //发送get请求
            httpResponse = httpClient.execute(get);
            try{
                //response实体
                HttpEntity entity = httpResponse.getEntity();
                if (null != entity){
                    System.out.println("响应状态码:"+ httpResponse.getStatusLine());
                    System.out.println("-------------------------------------------------");
                    System.out.println("响应内容:" + EntityUtils.toString(entity));
                    System.out.println("-------------------------------------------------");
                }
            }
            finally{
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }


}  